package com.smallyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smallyang.constants.SystemConstants;
import com.smallyang.domain.dto.FriendDTO;
import com.smallyang.domain.entity.Tag;
import com.smallyang.domain.vo.LinkVo;
import com.smallyang.domain.ResponseResult;
import com.smallyang.domain.entity.Link;
import com.smallyang.domain.vo.PageVo;
import com.smallyang.mapper.LinkMapper;
import com.smallyang.service.LinkService;
import com.smallyang.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2023-05-14 15:12:06
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult getAllLink() {
        //查询所有审核通过的
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NOTMAL);
        List<Link> links = list(queryWrapper);
        //转换成Vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult<PageVo> listFriendBackVO(Integer pageNum, Integer pageSize, FriendDTO friendDTO) {
        //分页查询
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        //判断name是否为空 为空才会进行下面的判断
        queryWrapper.like(StringUtils.hasText(friendDTO.getName()), Link::getName, friendDTO.getName());
        queryWrapper.eq(StringUtils.hasText(friendDTO.getStatus()), Link::getStatus, friendDTO.getStatus());

        Page<Link> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, queryWrapper);
        //封装数据返回
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);

    }
}

